package logic

import (
	"context"
	"github.com/pkg/errors"
	"yangming-college/app/usercenter/cmd/v1/model"
	"yangming-college/app/usercenter/cmd/v1/rpc/internal/svc"
	"yangming-college/app/usercenter/cmd/v1/rpc/pb"
	"yangming-college/app/usercenter/cmd/v1/rpc/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListCasPathLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListCasPathLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCasPathLogic {
	return &ListCasPathLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListCasPathLogic) ListCasPath(in *pb.ListCasPathReq) (*pb.ListCasPathResp, error) {
	// todo: add your logic here and delete this line
	var casbinPath []*model.CasbinPath
	var total int64
	err := l.svcCtx.DB.Table("casbin_path").Limit(int(in.Limit)).Offset(int((in.Offset - 1) * in.Limit)).Count(&total).Find(&casbinPath).Error
	if err != nil {
		return nil, errors.Wrapf(err, "ListCasPath")
	}
	rep := make([]*usercenter.CasPath, len(casbinPath), len(casbinPath))
	for i, path := range casbinPath {
		rep[i] = &usercenter.CasPath{
			Path:        path.Path,
			Method:      path.Method,
			Description: path.Description,
		}
	}
	return &pb.ListCasPathResp{
		List: rep,
		Num:  total,
	}, nil
}
